package bulk_param.modele;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;
 
public class ReadCVS {
	
  public Map<String, String> maps = new HashMap<String, String>();
  private GetDesktopPath path = new GetDesktopPath();
  public File csvFile = new File(path.getPath()+"/Contact_Num/prefixes_internationaux.txt");
  public BufferedReader br = null;
  public InputStreamReader isr =null; 
  public String line = "";
  public String indicatif= " ";
  public String cvsSplitBy = ";";
 
  public ReadCVS(){  
  }
  
  //RECUPERATION INDICATIF PAYS
  public String indicatifDictionary(String pays) {	
	try {
		isr= new InputStreamReader(new FileInputStream(csvFile), "UTF-8");
		br = new BufferedReader(isr);
		while ((line = br.readLine()) != null) {
			// use comma as separator
			String[] country = line.split(cvsSplitBy);
			maps.put(country[0], country[1]);
		}
		//loop map
		for (Map.Entry<String, String> entry : maps.entrySet()) {			
			if (entry.getValue().equalsIgnoreCase(pays)){
				indicatif= entry.getKey();
				System.out.println("code : "+indicatif +" Valeur : "+entry.getValue());
			}
		}
 
	} catch (FileNotFoundException e) {
		JOptionPane.showMessageDialog(null,"Le fichier des 'prefixes_internationaux'\nest introuvable","Attention",JOptionPane.WARNING_MESSAGE);
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (br != null) {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
	System.out.println("Done");
	return indicatif;
  }
  
  //RECUPERATION COMBOBOX PAYS
  public ArrayList<String> countryDictionary() {
	ArrayList<String> maList =new ArrayList<String>();
	try {
		isr= new InputStreamReader(new FileInputStream(csvFile), "UTF-8");
		br = new BufferedReader(isr);
		while ((line = br.readLine()) != null) {
			// use comma as separator
			String[] country = line.split(cvsSplitBy);
			maList.add(country[1]);
		} 
	} catch (FileNotFoundException e) {
		JOptionPane.showMessageDialog(null,"Le fichier des 'prefixes_internationaux'\nest introuvable","Attention",JOptionPane.WARNING_MESSAGE);
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (br != null) {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	System.out.println("Done");
	return maList;
  }
 
}

